package com.deram.ctin.serv.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.deram.ctin.common.utils.ArrayHandleUtils;
import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.dependencies.serv.utils.MybatisUtils;
import com.deram.ctin.serv.business.entity.BusinessInfo;
import com.deram.ctin.serv.business.filter.BusinessInfoFilter;
import com.deram.ctin.serv.business.mapper.BusinessInfoMapper;
import com.deram.ctin.serv.business.service.BusinessInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jtt
 * @version 1.0, 2017/9/28
 */
@Service
public class BusinessInfoServiceImpl implements BusinessInfoService {

    @Autowired
    BusinessInfoMapper businessInfoMapper;

    @Override
    public BusinessInfo find(Long enterId) {
        ValidateUtils.isId(enterId, "企业ID");
        BusinessInfoFilter filter = new BusinessInfoFilter();
        filter.setEnterId(enterId);
        EntityWrapper<BusinessInfo> ew = createEntityWrapper(filter);
        return businessInfoMapper.selectOneByWrapper(ew);
    }

    @Override
    public BusinessInfo findByYear(Long enterId, Integer year) {
        ValidateUtils.isId(enterId, "企业ID");
        ValidateUtils.notNull(year, "年份");
        BusinessInfoFilter filter = new BusinessInfoFilter();
        filter.setEnterId(enterId);
        filter.setYear(year);
        filter.setHasYear(true);
        EntityWrapper<BusinessInfo> ew = createEntityWrapper(filter);
        return businessInfoMapper.selectOneByWrapper(ew);
    }

    @Override
    public List<BusinessInfo> list(BusinessInfoFilter filter) {
        EntityWrapper<BusinessInfo> ew = createEntityWrapper(filter);
        List<BusinessInfo> result = businessInfoMapper.selectList(ew);
        return result;
    }


    @Override
    public BusinessInfo save(BusinessInfo param) {
        BusinessInfo search = find(param.getEnterId());
        if (null == search) {
            businessInfoMapper.insert(param);
        } else {
            businessInfoMapper.updateById(param);
        }
        return param;
    }

    @Override
    public void insertList(List<BusinessInfo> list) {
        if (null != list && !list.isEmpty()) {
            businessInfoMapper.batchInsert(list);
        }
    }

    @Override
    public void delete(Long id) {
        BusinessInfo search = find(id);
        BusinessUtils.notNull(search, "工商信息不存在");
        businessInfoMapper.deleteById(search);
    }

    @Override
    public List<Integer> listYear(Long enterId) {
        BusinessInfoFilter filter = new BusinessInfoFilter();
        filter.setSelectBy("DISTINCT year");
        filter.setEnterId(enterId);
        filter.setHasYear(true);
        filter.setOrderBy("year desc");
        List<BusinessInfo> list = list(filter);
        return ArrayHandleUtils.readAttrList(list, "year");
    }

    private EntityWrapper<BusinessInfo> createEntityWrapper(BusinessInfoFilter filter) {
        BusinessInfo entity = new BusinessInfo();
        entity.setEnterId(filter.getEnterId());
        entity.setYear(filter.getYear());
        EntityWrapper<BusinessInfo> ew = MybatisUtils.createEntityWrapper(entity, filter);
        if(null != filter.getHasYear()){
            if(filter.getHasYear()){
                ew.isNotNull("year");
            } else {
                ew.isNull("year");
            }
        }
        return ew;
    }
}
